package nancal.mp.service.jenkins

import nancal.mp.db.mongo.PipelineTemplateEnum
import nancal.mp.db.mongo.entity.dev.AppDeploySetting
import nancal.mp.db.mongo.entity.dev.JenkinsLog
import nancal.mp.db.mongo.extend.getAppDeploySetting
import nancal.mp.db.mongo.mor

class JenkinsPublishJob(
    val task: JenkinsLog = JenkinsLog()
) {
    val appDeploySetting: AppDeploySetting
        get() {
            return mor.dev.appDeploySetting.getAppDeploySetting(task.buildInfo.targetEnv)
        }

    val nginxDefFileName = "res/nginx.conf"

    var buildStartShell = ".ops.build-info.sh";

    private fun getJenkinsScript(): JenkinsScript {
        if (this.appDeploySetting.jenkinsServer.usePipeline) {
            return JenkinsPipeLineStyleScript(this.task);
        }
        return JenkinsCmdStyleScript(this.task);
    }

    /**
     * 标准流水线。
     */
    fun getStandardPipeLine(): String {
        if (this.appDeploySetting.jenkinsServer.jenkinsHttpUrl.isEmpty()) {
            throw RuntimeException("jenkinsServer.host未设置！")
        }
        var jenkins = getJenkinsScript();

        if (this.task.buildInfo.isLib) {
            return jenkins.getLibJenkinsPipeLine();
        }

        if (this.appDeploySetting.pipelineTemplate == PipelineTemplateEnum.K8s) {
            return jenkins.getK8sJenkinsPipeLine();
        }

        if (this.appDeploySetting.pipelineTemplate == PipelineTemplateEnum.Docker) {
            return jenkins.getDockerJenkinsPipeLine();
        }

        return jenkins.getNativeJenkinsPipeLine();
    }

}